package Server;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.codehaus.jettison.json.JSONObject;

import Server.BusinessObjects.Highscore;



@Path("/highscore")
public class HighscoreResource {

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/getHighscoreToCategory/{scenId}/{catId}/{userId}")
	public String getHighscoreToCategory(@PathParam("scenId") int scenId,
			@PathParam("catId") int catId, @PathParam("userId") int userId) {
		JSONObject o = new JSONObject();
		System.out.println("Aufruf");
		try {
			int points = 0;
			List<Highscore> lh = Highscore.getHighscoreForUserAndCat(userId,
					scenId, catId);

			for (Highscore h1 : lh) {
				points += h1.getPoints();
			}

			o.put("UserId", userId);
			o.put("ScenId", scenId);
			o.put("CatId", catId);
			o.put("Points", points);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return o.toString();
	}

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/getTotalHighscore/{scenId}/{userId}")
	public String getTotalHighscore(@PathParam("scenId") int scenId,
			@PathParam("userId") int userId) {
		JSONObject o = new JSONObject();
		try {
			int points = 0;
			List<Highscore> lh = Highscore.getHighscoreForUserAndScen(userId,
					scenId);

			for (Highscore h1 : lh) {
				points += h1.getPoints();
			}

			o.put("UserId", userId);
			o.put("ScenId", scenId);
			o.put("Points", points);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return o.toString();
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/getHighscoreInformationToCategory/{userId}/{scenId}/{catId}")
	public String getHighscoreInformationToCategory(@PathParam("scenId") int scenId,
			@PathParam("userId") int userId, @PathParam("catId") int catId) {
		JSONObject o = new JSONObject();
		try {
			int bestHighscore = Highscore.getBestHighscoreInCategory(scenId, catId);
			
			int myStanding = Highscore.getStandingToUserInCategory(userId, scenId, catId);

			o.put("bestHighscore", bestHighscore);
			o.put("myStanding", myStanding);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return o.toString();
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/getHighscoreInformationToScenario/{userId}/{scenId}")
	public String getHighscoreInformationToScenario(@PathParam("scenId") int scenId,
			@PathParam("userId") int userId) {
		JSONObject o = new JSONObject();
		try {
			int bestHighscore = Highscore.getBestHighscoreInScenario(scenId);
			
			int myStanding = Highscore.getStandingToUserInScenario(userId, scenId);

			o.put("bestHighscore", bestHighscore);
			o.put("myStanding", myStanding);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return o.toString();
	}
}